导航菜单
首页 >  Delphi JSON  > GitHub

GitHub

Json Data Objects

This Delphi unit contains a JSON parser that supports Delphi 2009-10Seattle and the platformsWin32, Win64 and ARM Android (MacOS and iOS may work).

Clone with GIT> git clone git@github.com:ahausladen/JsonDataObjects.git

or

> git clone https://github.com/ahausladen/JsonDataObjects.git

This will get you the Json Data Objects repository.

How to installClone the JsonDataObjects repositoryAdd the JsonDataObjects.pas unit to your project.FeaturesFast dual JSON parser for parsing UTF8 and UTF16 without conversionAutomatic creation of arrays and objectsEasy access mode with implicit operatorsCompact and formatted output modesVariants supportNull can be auto-typecasted to a value type if JsonSerializationConfig.NullConvertsToValueTypes is set to TrueProgress callback support for loading large JSON stringsWin32, Win64 and ARM Android support (MacOS and iOS may work)Usage

Simple example

var Obj: TJsonObject;begin Obj := TJsonObject.Parse('{ "foo": "bar", "array": [ 10, 20 ] }') as TJsonObject; tryShowMessage(Obj['foo']);ShowMessage(IntToStr(Obj['array'].Count));ShowMessage(IntToStr(Obj['array'].Items[0]));ShowMessage(IntToStr(Obj['array'].Items[1])); finallyObj.Free; end;end;

Filling and serializing JSON objects

var Obj, ChildObj: TJsonObject;begin Obj := TJsonObject.Create; try// easy accessObj['foo'] := 'bar';// normal (and faster) accessObj.S['bar'] := 'foo';// automatic array creation, Obj is the owner of 'array'Obj.A['array'].Add(10);Obj.A['array'].Add(20);// automatic object creation, 'array' is the owner of ChildObjChildObj := Obj.A['array'].AddObject;ChildObj['value'] := 12.3;// automatic array creation, ChildObj is the owner of 'subarray'ChildObj.A['subarray'].Add(100);ChildObj.A['subarray'].Add(200);ShowMessage(Obj.ToJSON({Compact:=}False)); finallyObj.Free; end;{"foo": "bar","bar": "foo","array": [10,20,{"value": 12.3,"subarray": [100,200]}]}

Copying JSON objects with Assign

var Obj, ClonedObj: TJsonObject;begin Obj := TJsonObject.ParseUtf8('{ "foo": [ "bar", {}, null, true, false, { "key": "value" } ] }') as TJsonObject; tryClonedObj := TJsonObject.Create;try // Make a copy of Obj ClonedObj.Assign(Obj); ShowMessage(ClonedObj.ToJSON(False));finally ClonedObj.Free;end; finallyObj.Free; end;end;{"foo": ["bar",{},null,true,false,{"key": "value"}]}

相关推荐: